{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9c7fe508",
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "bytes(7)\n",
    "\n",
    "bin(7)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3342c3fc",
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "nums = [4, 5, 6]\n",
    "msg = \"Numbers: {0}, {1}, {2}\".format(nums[0], nums[1], nums[2])\n",
    "print(msg)\n",
    "print(f'Numbers: {nums[0]}, {1}, {2}')\n",
    "print('%d' % nums[0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fdff12cf",
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "ages = {\"Dave\": 24, \"Mary\":42, \"John\":58}\n",
    "print(\"Mary\" in ages)      # True\n",
    "print(\"Will\" not in ages)  # Trueages.get(\"Dave\")\n",
    "ages.get(\"Hurry\")          # 返回None\n",
    "ages.get(\"Hurry\", 34)      # 返回34"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7130355a",
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "print(\"1024 * 768 = \", 1024 * 768)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1ec680bc",
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "print('I\\'m learning\\nPython.')\n",
    "print(\"I'm learning \\nPython\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "575fadaf",
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "# 浅拷贝\n",
    "a = [1, 2, 3]\n",
    "b = a\n",
    "print('a = ', a, \"\\tid(a) = \", id(a))\n",
    "print('b = ', b, \"\\tid(b) = \", id(b))\n",
    "a.append(4)\n",
    "print('a = ', a, \"\\tid(a) = \", id(a))\n",
    "print('b = ', b, \"\\tid(b) = \", id(b))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cb5fd4b3",
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "a = [1, 2, 3]\n",
    "b = a\n",
    "print('a = ', a, \"\\tid(a) = \", id(a))\n",
    "print('b = ', b, \"\\tid(b) = \", id(b))\n",
    "a = [2, 3 , 4]\n",
    "print('a = ', a, \"\\tid(a) = \", id(a))\n",
    "print('b = ', b, \"\\tid(b) = \", id(b))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4bb71407",
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "a = \"str\"\n",
    "b = a\n",
    "print('a = ', a, \"\\tid(a) = \", id(a))\n",
    "print('b = ', b, \"\\tid(b) = \", id(b))\n",
    "a.join('l')\n",
    "print('a = ', a, \"\\tid(a) = \", id(a))\n",
    "print('b = ', b, \"\\tid(b) = \", id(b))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "93cd8f30",
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "'中'.encode('utf-8')\n",
    "b'\\xe4\\xb8\\xad'.decode('utf-8')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "eed1936e",
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "print('%2d-%02d' % (3, 1))\n",
    "print('%.2f' % 3.1415926)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "45f80217",
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "'Hello, %s' % 'world'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e16b2ecc",
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "sum = 0\n",
    "for i in range(1, 101):\n",
    "    sum += i;\n",
    "print(sum)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e7e04185",
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "a = [1, 2, 3]\n",
    "for i in a:\n",
    "    print(i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "38265bf4",
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "n = 0\n",
    "while n < 10:\n",
    "    n = n + 1\n",
    "    if n % 2 == 0: # 如果n是偶数，执行continue语句\n",
    "        continue # continue语句会直接继续下一轮循环，后续的print()语句不会执行\n",
    "    print(n)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4cfa230d",
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "n = 1\n",
    "while n <= 100:\n",
    "    if n > 10: # 当n = 11时，条件满足，执行break语句\n",
    "        break # break语句会结束当前循环\n",
    "    print(n)\n",
    "    n = n + 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b2580362",
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "d = {'Michael': 95, 'Bob': 75, 'Tracy': 85}\n",
    "d['Michael']\n",
    "d['Adam'] = 67\n",
    "for k, v in d.items():\n",
    "    print(k, v)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b971ff5f",
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "print(\"str\".join(\"dfeaf\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2eceeb20",
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "# *, 后面是命名关键字参数, *d 可变参数, **kw 关键字参数\n",
    "def f1(a, b, c=0, *, d, **kw):\n",
    "    print('a =', a, 'b =', b, 'c =', c, 'd =', d, 'kw =', kw)\n",
    "\n",
    "def f2(a, b, c=0, *e, d, **kw):\n",
    "    print('a =', a, 'b =', b, 'c =', c, 'd =', d, 'kw =', kw)\n",
    "\n",
    "# f2(1,2,3)\n",
    "args = (1, 2, 3)\n",
    "kw = {'d': 88, 'x': '#'}\n",
    "f1(*args, **kw)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "bebb24f3",
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "L = list(range(1, 100))[::2]\n",
    "L"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9660b342",
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "list(range(1,100,2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d1500394",
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "# 切片\n",
    "lst = list(range(1, 10))\n",
    "# lst[:]\n",
    "lst[::-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0cbfc8c2",
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "# enumerate() 生成下标\n",
    "for i, value in enumerate(['A', 'B', 'C']):\n",
    "    print(i, value)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5316b905",
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "# 列表生成式\n",
    "[i*j for i in range(10) for j in range(0, i+1)]\n",
    "[x * x for x in range(1, 11) if x % 2 == 0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "12bf9122",
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "def fib(max):\n",
    "    n, a, b = 0, 0, 1\n",
    "    while n < max:\n",
    "        yield b\n",
    "        a, b = b, a + b\n",
    "        n = n + 1\n",
    "    return 'done'\n",
    "\n",
    "f = fib(5)\n",
    "for i in f:\n",
    "    print(i)\n",
    "# next(f)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "dc178f22",
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(1, 2, 3) {'name': 'Chena', 'time': 'now'}\n"
     ]
    }
   ],
   "source": [
    "def fun(*args, **kwargs):\n",
    "    print(args, kwargs)\n",
    "\n",
    "\n",
    "fun(1,2,3, name = 'Chena', time = 'now')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "303519ff",
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "10"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import functools\n",
    "\n",
    "# 偏函数 base 进制\n",
    "int2 = functools.partial(int, base = 2)\n",
    "\n",
    "int2(\"10\")\n",
    "\n",
    "max2 = functools.partial(max, 10)\n",
    "\n",
    "max2(2, 5, 7,1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1 4\n",
      "2 5\n",
      "3 6\n"
     ]
    }
   ],
   "source": [
    "x = [1, 2, 3]\n",
    "y = [4, 5, 6]\n",
    "\n",
    "for i, j in zip(x, y):\n",
    "    print(i, j)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "8e803070",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]\n",
      " [1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]\n",
      " [1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]\n",
      " [1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]\n",
      " [1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]\n",
      " [1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]\n",
      " [1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]\n",
      " [1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]\n",
      " [1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]\n",
      " [1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]]\n",
      "100 (10, 10) 2 float64\n"
     ]
    }
   ],
   "source": [
    "\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  },
  "vscode": {
   "interpreter": {
    "hash": "6a018d3a992d78c184ccc94ab54168c7b26325ed3c2283926339c3edbf5487e7"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
